﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Guline.SupportCMS.Models.Object;
using Guline.SupportCMS.Models.Context;
using Guline.SupportCMS.Models.ViewModel;
namespace Guline.SupportCMS.Controllers.Manage
{
    public class ArticleController : GulineBaseController
    {

        //
        // GET: /Article/
        public ActionResult jsonList()
        {
            var news = (from m in db.Articles orderby m.ID descending select new NewsJson() { ID=m.ID,Title=m.Title,CreateDate=m.created_Date,ReadCount=m.ReadCount,Image=m.Image,Enable=m.Enable}).ToList();
            return Json(news, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Index()
        {
          
            return View();
        }

        //
        // GET: /Article/Details/5

        public ActionResult Details(int id = 0)
        {
            Article article = db.Articles.Find(id);
            if (article == null)
            {
                return HttpNotFound();
            }
            return View(article);
        }

        //
        // GET: /Article/Create

        public ActionResult Create()
        {
            ViewBag.AccountID = new SelectList(db.Accounts, "ID", "UserName");
            ViewBag.CategoryID = db.Categories.Where(m => m.Enable && m.ParentID == null).Include(m => m.Childrens).OrderBy(m => m.ParentID).ToList();
            return View();
        }

        //
        // POST: /Article/Create

        [ValidateInput(false)]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Article article, string hdname)
        {
            if (ModelState.IsValid)
            {
                string[] idlist = hdname.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                article.created_Date = DateTime.Now;
                article.FullDes = HttpUtility.HtmlEncode(article.FullDes);
                if (User.Identity.IsAuthenticated)
                {
                   Account _UserInfo = db.Accounts.Where(m => m.UserName == User.Identity.Name).FirstOrDefault();
                    article.AccountID = _UserInfo.ID;
                }
                   
                db.Articles.Add(article);
                db.SaveChanges();
                foreach (string s in idlist)
                {
                    ArticleCategory ac = new ArticleCategory();
                    ac.ArticleID = article.ID;
                    ac.CategoryID = int.Parse(s);
                    db.ArticleCategories.Add(ac);
                }
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CategoryID = db.Categories.Where(m => m.Enable && m.ParentID == null).Include(m => m.Childrens).OrderBy(m => m.ParentID).ToList();
            ViewBag.AccountID = new SelectList(db.Accounts, "ID", "UserName", article.AccountID);
            return View(article);
        }

        //
        // GET: /Article/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Article article = db.Articles.Where(m => m.Enable && m.ID == id).Include(m => m.ArticleCategorys).FirstOrDefault();
            if (article == null)
            {
                return HttpNotFound();
            }
            article.FullDes = HttpUtility.HtmlDecode(article.FullDes);
            ViewBag.AccountID = new SelectList(db.Accounts, "ID", "UserName", article.AccountID);
            ViewBag.CategoryID = db.Categories.Where(m => m.Enable && m.ParentID == null).Include(m => m.Childrens).OrderBy(m => m.ParentID).ToList();
            return View(article);
        }

        //
        // POST: /Article/Edit/5
        [ValidateInput(false)]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Article article, string hdname)
        {
            if (ModelState.IsValid)
            {
                article.FullDes = HttpUtility.HtmlEncode(article.FullDes);
                db.Entry(article).State = EntityState.Modified;
                db.SaveChanges();
                string[] idlist = hdname.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                var items = db.ArticleCategories.Where(m => m.ArticleID == article.ID);
                foreach (var it in items)
                {
                    db.ArticleCategories.Remove(it);
                }
                db.SaveChanges();
                foreach (string s in idlist)
                {
                    ArticleCategory ac = new ArticleCategory();
                    ac.ArticleID = article.ID;
                    ac.CategoryID = int.Parse(s);
                    db.ArticleCategories.Add(ac);
                }
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.AccountID = new SelectList(db.Accounts, "ID", "UserName", article.AccountID);
            ViewBag.CategoryID = db.Categories.Where(m => m.Enable && m.ParentID == null).Include(m => m.Childrens).OrderBy(m => m.ParentID).ToList();
            return View(article);
        }

        //
        // GET: /Article/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Article article = db.Articles.Find(id);
            if (article == null)
            {
                return HttpNotFound();
            }
            return View(article);
        }

        //
        // POST: /Article/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Article article = db.Articles.Find(id);
            db.Articles.Remove(article);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        public ActionResult ajDelete(int id)
        {
            Article templatehook = db.Articles.Find(id);
            db.Articles.Remove(templatehook);
            db.SaveChanges();
            return Json(new { success = true });
        }
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}